home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / dspldrv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-11  |  5.7 KB  |  219 lines

  1. /*
  2.  *  DSPLDRV.C -- Display drivers
  3.  *
  4.  *  Copyright (C) 1990,1991 by Alef Null. All rights reserved.
  5.  *  Author(s): Jarkko Vuori, OH2LNS
  6.  *  Modification(s):
  7.  */
  8.  
  9. #include <graph.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include "dspldrv.h"
  15.  
  16.  
  17. struct {
  18.     struct xycoord lowerleft,
  19.            highright;
  20.     int        pixperchrx,
  21.            pixperchry;
  22.     int        dx,        // number of pixels in x direction
  23.            dy;        // number of pixels in y direction
  24.     int        colors;
  25. } dimensions;
  26. static int *prev;
  27.  
  28.  
  29. /*
  30.  * Initializes display
  31.  */
  32. int InitDspl(int dx, int dy) {
  33.     struct videoconfig config;
  34.     int            x, y;
  35.     char           buf[80];
  36.  
  37.     if (!(prev = calloc(dx, sizeof(int))) ||
  38.      _setvideomode(_VRES16COLOR) ||
  39.      _setvideomode(_HERCMONO)) {
  40.     _getvideoconfig(&config);
  41.  
  42.     dimensions.dx = dx;
  43.     dimensions.dy = dy;
  44.  
  45.     dimensions.colors = config.numcolors;
  46.  
  47.     /* calculate screen dimensions */
  48.     dimensions.lowerleft.xcoord = (config.numxpixels-dx)/2;
  49.     dimensions.lowerleft.ycoord = (config.numypixels-dy)/2;
  50.  
  51.     dimensions.highright.xcoord = dimensions.lowerleft.xcoord+dx;
  52.     dimensions.highright.ycoord = dimensions.lowerleft.ycoord+dy;
  53.  
  54.     dimensions.pixperchrx = config.numxpixels/config.numtextcols;
  55.     dimensions.pixperchry = config.numypixels/config.numtextrows;
  56.  
  57.     /* plot surrounding rectangle */
  58.     _setbkcolor(_BLUE); _setcolor(7); _settextcolor(7);
  59.     _rectangle(_GBORDER, dimensions.lowerleft.xcoord-1,
  60.                  dimensions.lowerleft.ycoord,
  61.                  dimensions.highright.xcoord,
  62.                  dimensions.highright.ycoord+1);
  63.     /* plot horizontal ticks */
  64.     for (x = dimensions.lowerleft.xcoord; x <= dimensions.highright.xcoord; x += dx/8) {
  65.         _moveto(x, dimensions.highright.ycoord+1);
  66.         _lineto(x, dimensions.highright.ycoord+1+dimensions.pixperchry/2);
  67.     }
  68.     /* plot vertical ticks */
  69.     for (y = dimensions.highright.ycoord; y >= dimensions.lowerleft.ycoord; y -= dy/8) {
  70.         _moveto(dimensions.highright.xcoord+1, y);
  71.         _lineto(dimensions.highright.xcoord+1+dimensions.pixperchrx, y);
  72.     }
  73.  
  74.     /* draw axis marks */
  75.     _settextposition(config.numtextrows/2, 80-4);
  76.     _outtext("[dB]");
  77.     _settextposition(config.numtextrows, 40-2);
  78.     _outtext("[Hz]");
  79.  
  80.     /* and finally static text fields */
  81.     sprintf(buf, "DSP CARD 3  FFT Spectrum Analyzer (%s)", __DATE__);
  82.     _settextposition(1, 1+(80-strlen(buf))/2);
  83.     _outtext(buf);
  84.  
  85.     _settextposition(10,1);
  86.     _outtext("Marker:");
  87.     _settextposition(6,1);
  88.     _outtext("N:");
  89.  
  90.     return(0);
  91.     } else
  92.     return (-1);
  93. }
  94.  
  95.  
  96. /*
  97.  * Plot one data value
  98.  */
  99. void PlotPoint(int x, int y) {
  100.     static int last_y, prev_last_y;
  101.  
  102.     if (x) {
  103.     /* first erase */
  104.     _setcolor(0);
  105.     _moveto(dimensions.lowerleft.xcoord+x-1, dimensions.highright.ycoord-prev_last_y);
  106.     _lineto(dimensions.lowerleft.xcoord+x, dimensions.highright.ycoord-prev[x]);
  107.  
  108.     /* then draw */
  109.     _setcolor((dimensions.colors > 2) ? 3 : 7);
  110.     _moveto(dimensions.lowerleft.xcoord+x-1, dimensions.highright.ycoord-last_y);
  111.     _lineto(dimensions.lowerleft.xcoord+x, dimensions.highright.ycoord-y);
  112.     }
  113.  
  114.     prev_last_y = prev[x];
  115.     last_y    = y;
  116.     prev[x]    = y;
  117. }
  118.  
  119.  
  120. /* plot marker symbol */
  121. static void DrawMarkerSymbol(int type, int x, int y, int color) {
  122.     const int dim = 3;
  123.  
  124.     _setcolor(color);
  125.     if (type)
  126.     _rectangle(_GBORDER, dimensions.lowerleft.xcoord+x-dim, dimensions.highright.ycoord-(y-dim),
  127.                  dimensions.lowerleft.xcoord+x+dim, dimensions.highright.ycoord-(y+dim));
  128.     else
  129.     _ellipse(_GBORDER, dimensions.lowerleft.xcoord+x-dim, dimensions.highright.ycoord-(y-dim),
  130.                  dimensions.lowerleft.xcoord+x+dim, dimensions.highright.ycoord-(y+dim));
  131. }
  132.  
  133.  
  134. /* plot one marker */
  135. static void DrawMarker(int type, int line, int prev_x, int prev_y, int x, int y, double freq, double amplitude) {
  136.     char buf[80];
  137.  
  138.     _settextwindow(line,  1,
  139.            line+1,10);
  140.  
  141.     /* then erase previous marker */
  142.     if (prev_x) {
  143.     _clearscreen(_GWINDOW);
  144.     DrawMarkerSymbol(type, prev_x, prev_y, 0);
  145.     }
  146.  
  147.     /* and finally draw a new one */
  148.     if (x) {
  149.     DrawMarkerSymbol(type, x, y, (dimensions.colors > 2) ? 3 : 7);
  150.     sprintf(buf, "%5.0lf Hz\n %2.1lf dB", freq,  amplitude);
  151.     _outtext(buf);
  152.     }
  153. }
  154.  
  155.  
  156. /*
  157.  * Plot card status
  158.  */
  159. void PlotStatus(long N, long samplef) {
  160.     static long prev_samplef;
  161.     int     x;
  162.     char    buf[80];
  163.  
  164.     /* update N display */
  165.     _settextwindow(7, 1,
  166.            7, 10);
  167.     sprintf(buf, "%5ld", N);
  168.     _clearscreen(_GWINDOW);
  169.     _outtext(buf);
  170.  
  171.     /* update frequency scale if needed */
  172.     if (samplef != prev_samplef) {
  173.     _settextwindow(1, 1,
  174.                30, 80);
  175.     for (x = dimensions.lowerleft.xcoord; x <= dimensions.highright.xcoord; x += dimensions.dx/8) {
  176.         sprintf(buf, "%5d", (int)(samplef*(x-dimensions.lowerleft.xcoord)/1024));
  177.         _settextposition(dimensions.highright.ycoord/dimensions.pixperchry+2, x/dimensions.pixperchrx-3);
  178.         _outtext(buf);
  179.     }
  180.  
  181.     prev_samplef = samplef;
  182.     }
  183. }
  184.  
  185.  
  186. /*
  187.  * Plot marker
  188.  */
  189. void PlotMarker(int x1, int y1, double freq1, double amplitude1,
  190.         int x2, int y2, double freq2, double amplitude2) {
  191.     static int prev_x1, prev_y1,prev_x2, prev_y2;
  192.     char       buf[80];
  193.  
  194.     if (prev_x1 != x1 || prev_y1 != y1) {
  195.     DrawMarker(1, 11, prev_x1, prev_y1, x1, y1, freq1, amplitude1);
  196.     prev_x1 = x1; prev_y1 = y1;
  197.     }
  198.  
  199.     if (prev_x2 != x2 || prev_y2 != y2) {
  200.     DrawMarker(0, 13, prev_x2, prev_y2, x2, y2, freq2, amplitude2);
  201.     prev_x2 = x2; prev_y2 = y2;
  202.     }
  203.  
  204.     _settextwindow(16, 1,
  205.            16, 10);
  206.     _clearscreen(_GWINDOW);
  207.     sprintf(buf, "%+2.1lf dB", amplitude1-amplitude2);
  208.     _outtext(buf);
  209. }
  210.  
  211.  
  212. /*
  213.  * releases display
  214.  */
  215. void ReleaseDspl() {
  216.     _setvideomode(_DEFAULTMODE);
  217.     free(prev);
  218. }
  219.